package com.ssky.bean.validator.core;

import com.ssky.bean.validator.core.exception.VldException;

import java.util.Map;

/**
 * @Author: ssky
 * @Date: 2021/07/23
 * @Description:
 */
public class FieldValueGetHandler implements IFieldValueGetHandler {

    private Map<String, IFullFieldValueGetter> fullFieldValueGetterMap;

    private Map<String, IFieldValueGetter> localFieldValueGetterMap;

    private Map<String, IFieldValueGetter> thisFieldValueGetterMap;


    public FieldValueGetHandler(Map<String, IFullFieldValueGetter> fullFieldValueGetterMap) {
        this.fullFieldValueGetterMap = fullFieldValueGetterMap;
    }

    public FieldValueGetHandler(Map<String, IFullFieldValueGetter> fullFieldValueGetterMap, Map<String, IFieldValueGetter> localFieldValueGetterMap, Map<String, IFieldValueGetter> thisFieldValueGetterMap) {
        this.fullFieldValueGetterMap = fullFieldValueGetterMap;
        this.localFieldValueGetterMap = localFieldValueGetterMap;
        this.thisFieldValueGetterMap = thisFieldValueGetterMap;
    }

    /**
     * 获取当前属性的子属性的值
     *
     * @param thisFeldName
     * @param thisObj      属性的所属对象
     * @return
     */
    @Override
    public Object getThisFv(String thisFeldName, Object thisObj) {
        IFieldValueGetter getter = thisFieldValueGetterMap.get(thisFeldName);
        if (getter != null) {
            return getter.getV(thisObj);
        } else {
            throw new VldException("IFieldValueGetter(thisFeldName:" + thisFeldName + ") not found, please check syntax for errors!");
        }
    }

    /**
     * 获取当前属性所属对象的子属性值
     *
     * @param localFieldName
     * @param localObj       属性的所属对象
     * @return
     */
    @Override
    public Object getLocalFv(String localFieldName, Object localObj) {
        IFieldValueGetter getter = localFieldValueGetterMap.get(localFieldName);
        if (getter != null) {
            return getter.getV(localObj);
        } else {
            throw new VldException("IFieldValueGetter(localFieleName:" + localFieldName + ") not found, please check syntax for errors!");
        }
    }

    @Override
    public Object getFfv(String fullFieldName, Object bean) {
        IFullFieldValueGetter getter = fullFieldValueGetterMap.get(fullFieldName);
        if (getter != null) {
            return getter.getV(bean);
        } else {
            throw new VldException("IFullFieldValueGetter (fullFieldName:" + fullFieldName + ") not found, please check syntax for errors!");
        }
    }

    @Override
    public Object getExpVaule(ValueExp valueExp, Object v, Object l, Object b, IValidatorContext context) {
        if (valueExp.isBean()) {
            return getFfv(valueExp.vExp, b);
        } else if (valueExp.isSelf()) {
            return v;
        } else if (valueExp.isConstant()) {
            return valueExp.vExp;
        } else if (valueExp.isLocal()) {
            return getLocalFv(valueExp.vExp, l);
        } else if (valueExp.isThis()) {
            return getThisFv(valueExp.vExp, v);
        } else if (valueExp.isContext()) {
            return context.getAttribute(valueExp.vExp);
        }
        return null;
    }
}
